package sk.vasicek.drinkmanager.activity;

import sk.gtug.hackatlon.drunkdroid.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class DrinkManagerActivity extends Activity {
	/** Called when the activity is first created. */

	private Button beerButton;
	private Button shotButton;
	private Button wineButton;
	private TextView beerCountText;
	private TextView shotCountText;
	private TextView wineCountText;
	private int beerCount = 0;
	private int shotCount = 0;
	private int wineCount = 0;
	private SeekBar shotBar;
	private SeekBar beerBar;
	private SeekBar wineBar;
	private Button checkStatsButton;
	private TextView finalPriceText;
	private EditText beerPrice;
	private EditText shotPrice;
	private EditText winePrice;
	private RatingBar drunkRating;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.drinkmanager);
		// beer controllers
		this.beerButton = (Button) this.findViewById(R.id.beerButton);
		this.beerPrice = (EditText) this.findViewById(R.id.beerPrice);
		this.beerCountText = (TextView) this.findViewById(R.id.beerCount);
		this.beerBar = (SeekBar) this.findViewById(R.id.beerBar);
		// shot controllers
		this.shotButton = (Button) this.findViewById(R.id.shotButton);
		this.shotPrice = (EditText) this.findViewById(R.id.shotPrice);
		this.shotCountText = (TextView) this.findViewById(R.id.shotCount);
		this.shotBar = (SeekBar) this.findViewById(R.id.shotBar);
		// wine controllers
		this.wineButton = (Button) this.findViewById(R.id.wineButton);
		this.winePrice = (EditText) this.findViewById(R.id.winePrice);
		this.wineCountText = (TextView) this.findViewById(R.id.wineCount);
		this.wineBar = (SeekBar) this.findViewById(R.id.wineBar);
		// stats controllers
		this.checkStatsButton = (Button) this.findViewById(R.id.checkStatsButton);
		this.drunkRating = (RatingBar) this.findViewById(R.id.drunkRating);
		this.finalPriceText = (TextView) this.findViewById(R.id.finalPriceText);

		this.beerButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				DrinkManagerActivity.this.beerCount++;
				DrinkManagerActivity.this.beerCountText.setText(String.valueOf(DrinkManagerActivity.this.beerCount));
				DrinkManagerActivity.this.beerBar.setProgress(DrinkManagerActivity.this.beerCount);
			}
		});

		this.beerBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				DrinkManagerActivity.this.beerCount = DrinkManagerActivity.this.beerBar.getProgress();
				DrinkManagerActivity.this.beerCountText.setText(String.valueOf(DrinkManagerActivity.this.beerCount));
				// DrinkCounterActivity.this.beerBar.setProgress(DrinkCounterActivity.this.beerCount);

			}
		});
		this.shotBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				DrinkManagerActivity.this.shotCount = DrinkManagerActivity.this.shotBar.getProgress();
				DrinkManagerActivity.this.shotCountText.setText(String.valueOf(DrinkManagerActivity.this.shotCount));
				// DrinkCounterActivity.this.beerBar.setProgress(DrinkCounterActivity.this.beerCount);

			}
		});
		this.shotButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				DrinkManagerActivity.this.shotCount++;
				DrinkManagerActivity.this.shotCountText.setText(String.valueOf(DrinkManagerActivity.this.shotCount));
				DrinkManagerActivity.this.shotBar.setProgress(DrinkManagerActivity.this.shotCount);
			}
		});
		this.wineBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				DrinkManagerActivity.this.wineCount = DrinkManagerActivity.this.wineBar.getProgress();
				DrinkManagerActivity.this.wineCountText.setText(String.valueOf(DrinkManagerActivity.this.wineCount));
				// DrinkCounterActivity.this.beerBar.setProgress(DrinkCounterActivity.this.beerCount);

			}
		});
		this.wineButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				DrinkManagerActivity.this.wineCount++;
				DrinkManagerActivity.this.wineCountText.setText(String.valueOf(DrinkManagerActivity.this.wineCount));
				DrinkManagerActivity.this.wineBar.setProgress(DrinkManagerActivity.this.wineCount);
			}
		});
		this.checkStatsButton.setOnClickListener(new OnClickListener() {

			private final float alcoholMassCoeficientBySex = new Float("0.68");
			private final float ethanolConcentration = new Float("0.8");
			private final float bodyWeight = new Float("75");
			float beerAlcoholConcentration = new Float("0.05");
			float shotAlcoholConcentration = new Float("0.39");
			float wineAlcoholConcentration = new Float("0.16");

			float alcoholConcentration = 0.0f;
			float drunkRating = this.alcoholConcentration * 100;

			@Override
			public void onClick(View v) {
				this.alcoholConcentration = (float) (((DrinkManagerActivity.this.beerCount * 0.5 * this.beerAlcoholConcentration * this.ethanolConcentration)
						+ (DrinkManagerActivity.this.shotCount * 0.05 * this.shotAlcoholConcentration * this.ethanolConcentration) + (DrinkManagerActivity.this.wineCount * 0.2
								* this.wineAlcoholConcentration * this.ethanolConcentration)) / (this.bodyWeight * this.alcoholMassCoeficientBySex));
				this.drunkRating = this.alcoholConcentration * 1000 * 1.2f;
				Log.v(this.getClass().getSimpleName(), "concentration=" + this.alcoholConcentration);

				float beerFinalPrice = new Float("0.0");
				float shotFinalPrice = new Float("0.0");
				float wineFinalPrice = new Float("0.0");
				if (DrinkManagerActivity.this.beerCount > 0 && DrinkManagerActivity.this.beerPrice != null && DrinkManagerActivity.this.beerPrice.getText().toString() != "") {
					try {
						beerFinalPrice = DrinkManagerActivity.this.beerCount * Float.parseFloat(DrinkManagerActivity.this.beerPrice.getText().toString().replaceAll(",", "."));
					} catch (NumberFormatException e) {
						Toast toast = Toast.makeText(DrinkManagerActivity.this.getApplicationContext(), "Please enter price for beer!", Toast.LENGTH_SHORT);
						toast.show();
					}
				}
				if (DrinkManagerActivity.this.shotCount > 0 && DrinkManagerActivity.this.shotPrice != null && DrinkManagerActivity.this.shotPrice.getText().toString() != "") {
					try {
						shotFinalPrice = DrinkManagerActivity.this.shotCount * Float.parseFloat(DrinkManagerActivity.this.shotPrice.getText().toString().replaceAll(",", "."));
					} catch (NumberFormatException e) {
						Toast toast = Toast.makeText(DrinkManagerActivity.this.getApplicationContext(), "Please enter price for shot!", Toast.LENGTH_SHORT);
						toast.show();
					}
				}
				if (DrinkManagerActivity.this.wineCount > 0 && DrinkManagerActivity.this.winePrice != null && DrinkManagerActivity.this.winePrice.getText().toString() != "") {
					try {
						wineFinalPrice = DrinkManagerActivity.this.wineCount * Float.parseFloat(DrinkManagerActivity.this.winePrice.getText().toString().replaceAll(",", "."));
					} catch (NumberFormatException e) {
						Toast toast = Toast.makeText(DrinkManagerActivity.this.getApplicationContext(), "Please enter price for wine!", Toast.LENGTH_SHORT);
						toast.show();
					}
				}
				float finalPrice = beerFinalPrice + shotFinalPrice + wineFinalPrice;
				DrinkManagerActivity.this.finalPriceText.setText(String.valueOf(finalPrice) + " €");
				DrinkManagerActivity.this.drunkRating.setRating(this.drunkRating);
				Toast.makeText(DrinkManagerActivity.this.getApplicationContext(), "Alcohol concentration is: " + String.valueOf(this.alcoholConcentration * 1000) + "promile", Toast.LENGTH_SHORT)
				.show();
			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = this.getMenuInflater();
		inflater.inflate(R.menu.optionmenu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.exit:
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("Are you sure you want to exit?")
			.setCancelable(false)
			.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					Toast.makeText(DrinkManagerActivity.this.getApplicationContext(), "Exiting drinks manager, cheers :)", Toast.LENGTH_SHORT).show();
					DrinkManagerActivity.this.finish();
				}
			})
			.setNegativeButton("No", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					dialog.cancel();
				}
			});
			AlertDialog alert = builder.create();
			alert.show();
			//			Toast.makeText(DrinkManagerActivity.this.getApplicationContext(), "Exiting drinks manager, cheers :)", Toast.LENGTH_SHORT).show();
			//						this.onDestroy();
			return true;
		case R.id.settings:
			Toast.makeText(DrinkManagerActivity.this.getApplicationContext(), "Here will be settings screen", Toast.LENGTH_SHORT).show();
			return true;
		case R.id.drinksgraph:
			Toast.makeText(DrinkManagerActivity.this.getApplicationContext(), "Here will be graphs screen", Toast.LENGTH_SHORT).show();
			return true;
		case R.id.drinksettings:
			Toast.makeText(DrinkManagerActivity.this.getApplicationContext(), "Here will be drink settings screen", Toast.LENGTH_SHORT).show();
			return true;
		case R.id.personalpreferences:
			Toast.makeText(DrinkManagerActivity.this.getApplicationContext(), "Here will be personal preferences screen", Toast.LENGTH_SHORT).show();
			return true;
		case R.id.history:
			Toast.makeText(DrinkManagerActivity.this.getApplicationContext(), "Here will be history screen", Toast.LENGTH_SHORT).show();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

}